﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class Port : System.Web.UI.Page
    {
        //protected DataTable dt;
        Phoenix.AppCode.Entities.Port port = new Phoenix.AppCode.Entities.Port();
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'/>" + "Cập nhật thông tin thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'/>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'/>" + "Thông tin chưa được cập nhật." + "</p></div><br  class='clear'/>";
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    //dt = Phoenix.AppCode.Bussiness.PortService.PortList();

                    if (Request.QueryString["Action"] == "Create" || Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "OnOff" || Request.QueryString["Action"] == "Delete")
                    {
                        add.Visible = false;
                        edit.Visible = false;
                        Load_ActionPort(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        #region  Load action Port
        private void Load_ActionPort(string action)
        {
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Create")
                {
                    add.Visible = true;
                    edit.Visible = false;
                    list.Visible = true;
                }
                else if (action == "Edit")
                {
                    add.Visible = false;
                    edit.Visible = true;
                    list.Visible = true;

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.PortService.PortList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    txtPortNameE.Text = dt_Edit.Rows[0]["PortName"].ToString();
                }
                else if (action == "Delete")
                {
                    try
                    {
                        port = Phoenix.AppCode.Bussiness.PortService.GetPortByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.PortService.Delete(port);
                        if (Request.Cookies["ddlPort"] != null)
                        {
                            Response.Cookies["ddlPort"].Expires = DateTime.Now;
                        }
                        lblWarning.Text = success;
                        //Response.Redirect("Port.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else if (action == "OnOff")
                {
                    try
                    {
                        port = Phoenix.AppCode.Bussiness.PortService.GetPortByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.PortService.OnOff(port);
                        lblWarning.Text = success;
                        //Response.Redirect("Port.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else
                {
                    add.Visible = false;
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                port.PortName = txtPortNameA.Text;
                port.PostUser = Session["UserName"].ToString();
                Phoenix.AppCode.Bussiness.PortService.Create(port);
                ClearAllFields();
                lblWarningA.Text = success;
                //Response.Redirect("Port.aspx?Action=Create");
            }
            catch
            {
                ClearAllFields();
                lblWarningA.Text = error;
                //Response.Redirect("Error.aspx");
            }
        }
        protected void btnCancelA_Click(object sender, EventArgs e)
        {
            ClearAllFields();
            lblWarningA.Text = info;
        }

        private void ClearAllFields()
        {
            txtPortNameA.Text = "";
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            port = Phoenix.AppCode.Bussiness.PortService.GetPortByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            port.PortName = txtPortNameE.Text;
            port.EditUser = Session["UserName"].ToString();
            Phoenix.AppCode.Bussiness.PortService.Edit(port);
            lblWarningE.Text = success;
            //Response.Redirect("Port.aspx?Action=Edit&ID=" + Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()) + "");
        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.PortService.PortList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            txtPortNameE.Text = dt_Edit.Rows[0]["PortName"].ToString();
            lblWarningE.Text = info;
        }
    }
}